package com.example.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.domain.ShoppingCart;
import com.example.mapper.ShoppingCartMapper;
import com.example.service.IShoppingCartService;
import com.example.service.ex.DeleteException;
import com.example.service.ex.InsertException;
import com.example.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车的相关业务功能
 */
@Service
public class ShoppingCartServiceImpl implements IShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    //添加菜品进购物车
    @Override
    public ShoppingCart add(ShoppingCart shoppingCart, Long id) {
        //再添加菜品时不知道他点击了多少次同一个菜品，
        //如果点击多次不可以在数据库中添加多次，所以要先查询一下，根据用户id和餐品id或套餐id
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, id);
        //由于不确定添加的是套餐还是菜品所以要进行判断一下
        lambdaQueryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        lambdaQueryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart shoppingCart1 = shoppingCartMapper.selectOne(lambdaQueryWrapper);
        //判断一下查询出来了数据没有
        if (shoppingCart1 != null) {
            //如果有数据就让其数量加1并将这个数据进行修改
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            //将数据进行修改
            int i = shoppingCartMapper.updateById(shoppingCart1);
            if (i <= 0) {
                throw new UpdateException("修改数据时产生未知的异常");
            }
        } else {
            //将其存储到购物车中，首先要为其添加用户id,设置其数量
            shoppingCart.setUserId(id);
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            int insert = shoppingCartMapper.insert(shoppingCart);
            shoppingCart1 = shoppingCart;
            if (insert <= 0) {
                throw new InsertException("插入数据时产生未知的异常");
            }
        }
        return shoppingCart1;
    }

    //减少菜品
    @Override
    public ShoppingCart sub(ShoppingCart shoppingCart, Long userId) {
        //创建条件构造器
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        lambdaQueryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        lambdaQueryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        //查询出该数据对象
        ShoppingCart shoppingCart1 = shoppingCartMapper.selectOne(lambdaQueryWrapper);
        shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
        Integer number = shoppingCart1.getNumber();
        //对餐品数量大于0时进行减少
        if (number > 0) {
            int i = shoppingCartMapper.updateById(shoppingCart1);
            if (i <= 0) {
                throw new UpdateException("数据进行修改时产生未知的异常");
            }
        } else {
            int i1 = shoppingCartMapper.deleteById(shoppingCart1);
            if (i1 <= 0) {
                throw new DeleteException("删除数据时产生未知的异常");
            }
        }
        return shoppingCart1;
    }
    //购物车展示信息过程中判断一下餐品数量有没有为0,有就没必要展示直接删除
    @Override
    public List<ShoppingCart> list (Long userId){
        //根据用户id查询出购物车中的数据信息
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(userId != null, ShoppingCart::getUserId, userId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(lambdaQueryWrapper);
        return shoppingCarts;
    }

    //清空购物车
    @Override
    public void clean(Long userId) {
        //创建条件构造器
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(userId!=null,ShoppingCart::getUserId,userId);
        //清空购物车
        int delete = shoppingCartMapper.delete(lambdaQueryWrapper);
        if (delete<=0){
            throw new DeleteException("删除数据时产生未知的异常");
        }
    }
}
